import javax.swing.JButton;
import javax.swing.JFrame;

public class HolePlacement extends BoardLayout {
	
	public static int NextHoleCompute(int currenthole, int nexthole,int pits[],int stonesleft[]){
			
		if(nexthole <=7){
			nexthole = RightSideCompute(nexthole,currenthole,stonesleft,pits);
		}
		else if (nexthole >=8){
			
			nexthole = LeftSideCompute(nexthole, currenthole, stonesleft, pits);
			//stonesleft[currenthole] -=1;
		}
		else{
			
			System.out.println("ERROR");
		}
		
	return nexthole;
			
		
	}
	public static int LeftSideCompute(int nexthole, int currenthole, int stonesleft[], int pits[]){
		
		if( nexthole == 8){
			
			nexthole = Pit2Adder(nexthole, currenthole, pits, stonesleft);
		}
		else{
			nexthole -= 1;
			stonesleft[currenthole] -= 1;
		}
		
		return nexthole;
	}
	
	public static int RightSideCompute(int nexthole,int currenthole, int stonesleft[], int pits[]){
		
		if (nexthole == 7){
			
			nexthole = Pit1Adder(nexthole,pits,currenthole,stonesleft);
			//nexthole = 14;
		}
		
		else{
			nexthole = nexthole + 1;
			stonesleft[currenthole] -=1;
		}
		
		return nexthole;
	}
	
	
	public static int Pit1Adder(int nexthole,int pits[],int currenthole, int stonesleft[]){
		
		pits[0] += 1;
		stonesleft[currenthole] -= 1;
		nexthole = 14;
		
		return nexthole;
		
	}
	
	
	public static int Pit2Adder(int nexthole, int currenthole, int pits[], int stonesleft[]){
		
		pits[1] += 1;
		stonesleft[currenthole] -=1;
		nexthole = 1;
		
		return nexthole;
	}
public static int InitialPitCheck(int nexthole, int pits[], int currenthole, int stonesleft[]){
		
		if( currenthole <= 7){
			if(currenthole ==7){
				nexthole = Pit1Adder(nexthole,pits,currenthole,stonesleft);
			}
			else{
				nexthole = currenthole + 1;
			}
		
		}
		else if (currenthole >= 8){
			if(currenthole == 8){
				nexthole = Pit2Adder(nexthole,currenthole,pits,stonesleft);
			}
			
			else{
				nexthole = currenthole - 1;
			}
		}	
		
		return nexthole;
	}
		public static void main(String[] args) {
			int currenthole;
			//int value;
			int initialvalue = 0;
			int nexthole;
			int lasthole = 0;
			int[] pits = new int[2];
			int[] stonesleft = new int[15];
			//String pit1status = new String();
			
			JFrame MainBoard = new JFrame();
			JButton HoleButton1 = new JButton("a");
			
			MainBoard.add(HoleButton1);
			MainBoard.setVisible(true);
			
			//currenthole = value;
			for (int i = 1; i < holevalue.length; i ++){
				
				holevalue[i] = i;
				//System.out.println(holevalue[i]);
			}
			
			currenthole = holevalue[3];
			
			
			
			
			for(int i = 1; i <=14; i ++){ //place intial stones in holes
				
				stonesleft[i] = 7;
			}
			
			//System.out.println(stonesleft[currenthole]);
			nexthole = InitialPitCheck(initialvalue,pits,currenthole,stonesleft);
			stonesleft[currenthole] -= 1;
			
			//System.out.println(nexthole);
			//System.out.println(stonesleft[currenthole]);
			
			while(stonesleft[currenthole] > 0)
				
			{
				stonesleft[nexthole] += 1;
				
				nexthole = NextHoleCompute(currenthole,nexthole,pits,stonesleft);
				//System.out.println(nexthole);
				
			}
			
			if(currenthole <= 7){
				if(nexthole ==8){
					lasthole = nexthole;
					stonesleft[lasthole] += 1;
					
				}
				else if(nexthole == 14){
					lasthole = 7;
					//pit1status = "Last Stone went in Pit1";
					
				}
				else{
				
				lasthole = nexthole + 1;
				}
				
				
			}	
				
			
			else{
				
				
				if(nexthole == 7){
					lasthole = nexthole;
					stonesleft[lasthole] += 1;
					
					
				}
				else if (nexthole == 1){
					lasthole = 8;
					//pit1status = "Last Stone went int Pit2";
					
				}
				else{
					lasthole = nexthole - 1;
					
				}
			}	
			
			
			
			//System.out.println("Last Hole " + lasthole + " " + stonesleft[lasthole] + " " + pit1status + '\n' + "StonesLeft " + stonesleft[currenthole] + '\n' + "Pit1 " + pits[0] + '\n' + "Pit2 " + pits[1]);
			for (int i =1; i < stonesleft.length; i ++){
				
				//System.out.println("Stones in Hole " + i + " are " + stonesleft[i]); 
				
			}
			
			//System.out.println("Stones in Pit1 are " + pits[0]);
			//System.out.println("Stones in Pit2 are " + pits[1]);

			

	}
	

}
